package com.hxxy.wyp.petsLover.common.rest.base.fronted;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Message;
import com.hxxy.wyp.petsLover.common.entities.MessageReply;
import com.hxxy.wyp.petsLover.common.services.base.backend.MessageReplyService;
import com.hxxy.wyp.petsLover.common.services.base.backend.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MessageFrontRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/8 14:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/messageFront")
public class MessageFrontRest extends RestBase {
    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageReplyService messageReplyService;

    @RequestMapping("/listOpen")
    @SaCheckLogin
    public Object listAllMessageFront() {
        LambdaQueryWrapper<Message> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Message::getStatus, "1");
        List<Message> messages = messageService.list(lambdaQueryWrapper);
        messages.stream().map((message) -> {
            LambdaQueryWrapper<MessageReply> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(MessageReply::getMessageId, message.getId());
            MessageReply one = messageReplyService.getOne(lambdaQueryWrapper1);
            message.setMessageReply(one);
            return message;
        }).collect(Collectors.toList());
        return ajaxSuccess(messages, "200", "查询成功!");
    }
}
